//
//  CustomNewsView.m
//  AmountBody
//
//  Created by smm on 2018/1/8.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "CustomNewsView.h"
#import "EssentialModel.h"


static const CGFloat View_Tag = 90000;
static const CGFloat Text_Tag = 100000;
@implementation CustomNewsView

-(void)loadUIForData
{
    NSMutableArray * needArr = [[NSMutableArray alloc]init];
    NSArray * nameArr = [NSArray arrayWithObjects:@"客户昵称：",@"客户名称：",@"身高：",@"体重：",@"电话：",@"客户单号：",@"邮箱：",@"备注：", nil];
    NSArray * mustArr = [NSArray arrayWithObjects:@"1",@"1",@"1",@"0",@"1",@"0",@"0",@"0", nil];
    NSArray * typeArr = [NSArray arrayWithObjects:@"0",@"0",@"1",@"1",@"0",@"0",@"0",@"0", nil];
    NSArray * valueArr = [NSArray arrayWithObjects:@"0",@"0",@"170",@"70",@"0",@"0",@"0",@"0", nil];
    NSArray * unitArr = [NSArray arrayWithObjects:@"0",@"0",@"cm",@"kg",@"0",@"0",@"0",@"0", nil];
    NSArray * uiTypeArr = [NSArray arrayWithObjects:@(UITypeForLeft),@(UITypeForRight),@(UITypeForCenter),@(UITypeForCenter),@(UITypeForLeft),@(UITypeForRight),@(UITypeForLeft),@(UITypeForRight), nil];
    for (int i=0; i<nameArr.count; i++) {
        EssentialModel * model = [[EssentialModel alloc]init];
        model.name = [nameArr objectAtIndex:i];
        model.isMust = [[mustArr objectAtIndex:i] boolValue];
        model.essentialType = [[typeArr objectAtIndex:i] intValue];
        model.value = [valueArr objectAtIndex:i];
        model.unit =[unitArr objectAtIndex:i];
        model.uiType = [[uiTypeArr objectAtIndex:i] intValue];
        model._id = [NSString stringWithFormat:@"%d",i];
        [needArr addObject:model];
    }
    _newsArray = [NSMutableArray arrayWithArray:needArr];
}
-(void)loadUIForCustomNewsView
{
    [self loadUIForData];
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }

    [self.superview layoutIfNeeded];  
    CGFloat viewW = self.frame.size.width;
    CGFloat n_viewH = 45;
    CGFloat r_viewH = 80;
    UIView * l_view = nil;
    for (int i=0; i<_newsArray.count; i++) {
        EssentialModel * model = [_newsArray objectAtIndex:i];
        CGFloat t_viewH = 0;
        if (model.essentialType == EssentialViewTypeForNomal) {
            t_viewH = n_viewH;
            UIView * view = [[UIView alloc]init];
            [self addSubview:view];
            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self);
                if (l_view) {
                    make.top.equalTo(l_view.mas_bottom);
                }else{
                    make.top.equalTo(self);
                }
                make.height.equalTo(@(t_viewH));
            }];
            view.tag = View_Tag+i;
            UILabel * nameLabel = [[UILabel alloc]init];//WithFrame:CGRectMake(0, t_viewH-Nomal_Label_H, viewW, Nomal_Label_H)
            [view addSubview:nameLabel];
            
            nameLabel.text = model.name;
            nameLabel.textColor = ShenZi_Color;
            nameLabel.font = Cell_Big_Font;
            [nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(view);
                make.top.equalTo(view).offset(t_viewH-Nomal_Label_H);
                make.height.equalTo(@(Nomal_Label_H));
            }];
            CGFloat biaoW = 5;
            CGFloat biaoH = biaoW;
            if (model.isMust) {
                UILabel * biaoLabel = [[UILabel alloc]init];//WithFrame:CGRectMake(nameLabel.frame.origin.x+nameLabel.frame.size.width, nameLabel.frame.origin.y-5, biaoW, biaoH)
                [view addSubview:biaoLabel];
                [biaoLabel setBianKuangAndColor:RGBColorAlpha(227, 65, 85, 1)];
                [biaoLabel setYuanJiaoForRadius:biaoH/2.0 andMasks:NO];
                [biaoLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(nameLabel.mas_right);
                    make.top.equalTo(nameLabel).offset(5);
                    make.height.equalTo(@(biaoW));
                    make.width.equalTo(@(biaoH));
                }];
                
            }
            CGFloat jianGeW = 85;
            UIView * lineView = [[UIView alloc]init];
            lineView.backgroundColor = RGBColorAlpha(235, 235, 235, 1);
            [view addSubview:lineView];
            [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(view).offset(jianGeW);
                make.right.equalTo(view);
                make.top.equalTo(@(t_viewH-1));
                make.height.equalTo(@(1));
               
            }];
            UITextField * textF = [[UITextField alloc]init];//WithFrame:CGRectMake(jianGeW, t_viewH-21, lineView.frame.size.width, 20)
            textF.borderStyle = UITextBorderStyleNone;
            textF.textColor = ShenZi_Color;
            textF.font = Cell_Big_Font;
            textF.delegate = self;
            textF.tag = Text_Tag+i;
            textF.returnKeyType = UIReturnKeyNext;
            if (i==_newsArray.count-1) {
                textF.returnKeyType = UIReturnKeyDone;
            }
            [view addSubview:textF];
            [textF mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(view).offset(jianGeW);
                make.width.equalTo(lineView);
                make.top.equalTo(@(t_viewH-21));
                make.height.equalTo(@(Nomal_Label_H));
                
            }];
            l_view = view;
        }else{
            CGFloat jianGeW = 85;
            t_viewH = r_viewH;
            RulerShowView * view = [[RulerShowView alloc]init];
            view.tag = View_Tag+i;
            [self addSubview:view];
            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self);
                if (l_view) {
                    make.top.equalTo(l_view.mas_bottom);
                }else{
                    make.top.equalTo(self);
                }
                make.height.equalTo(@(t_viewH));
            }];
            
            view.valueText.delegate  =self;
            view.valueText.tag = Text_Tag+i;
            [view reloadUI:model];
         
            if ([model.name isEqualToString:@"身高："]) {
//                view.rulerCustom.rulerType = RulerTypeForHeight;
                view.rulerCustom.min = 0;
                view.rulerCustom.max = 280;
//                [view.rulerCustom reDrawerRuler];
                [view.rulerCustom setValue:[model.value intValue] animated:YES];
            }else{
//                view.rulerCustom.rulerType = RulerTypeForWeight;
                view.rulerCustom.min = 0;
                view.rulerCustom.max = 500;
//                [view.rulerCustom reDrawerRuler];
                [view.rulerCustom setValue:[model.value intValue] animated:YES];
            }
            l_view = view;
        }
        
        
    }
    [l_view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-20);

    }];
    
}
//横屏
-(void)loadUIForCustomNewsViewHorizontal
{
    CGFloat n_viewH = 45;
    CGFloat r_viewH = 80;
    CGFloat jianGe = 30;
    UIView * l_view = nil;
    for (UIView * view in self.subviews) {
        CGFloat t_viewH = 0;
        int index =  (int)view.tag-View_Tag;
        
        l_view = [self viewWithTag:View_Tag+index-1];
        view.tag = View_Tag+index;
        EssentialModel * model = nil;
        if (index <=_newsArray.count-1 && index>=0) {
            model = [_newsArray objectAtIndex:index];
        }
        
        if (model.essentialType == EssentialViewTypeForNomal) {
            t_viewH = n_viewH;
            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self);
                if (l_view) {
                    make.top.equalTo(l_view.mas_bottom);
                }else{
                    make.top.equalTo(self);
                }
                make.height.equalTo(@(t_viewH));
            }];
        }else{
            CGFloat jianGeW = 85;
            t_viewH = r_viewH;

            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self);
                if (l_view) {
                    make.top.equalTo(l_view.mas_bottom);
                }else{
                    make.top.equalTo(self);
                }
                make.height.equalTo(@(t_viewH));
            }];
        }
     
    }
    [l_view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-40);
        
    }];
}
//竖屏
-(void)loadUIForCustomNewsViewVertical
{
    CGFloat n_viewH = 45;
    CGFloat r_viewH = 80;
    CGFloat jianGe = 30;
    UIView * l_view = nil;
    for (UIView * view in self.subviews) {
        int index =  (int)view.tag-View_Tag;
        l_view = [self viewWithTag:View_Tag+index-1];
        view.tag = View_Tag+index;
        EssentialModel * model = nil;
        if (index <=_newsArray.count-1 && index>=0) {
            model = [_newsArray objectAtIndex:index];
        }
        CGFloat t_viewH = 0;
        if (model.essentialType == EssentialViewTypeForNomal) {
            t_viewH = n_viewH;
            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                if (l_view) {
                    
                    if (model.uiType == UITypeForRight) {
                        make.top.equalTo(l_view);
                        make.left.equalTo(l_view.mas_right).offset(jianGe);
                        make.width.equalTo(self).multipliedBy(0.5).offset(-jianGe);
                    }else{
                        make.top.equalTo(l_view.mas_bottom);
                        make.left.equalTo(self);
                        if (model.uiType == UITypeForCenter) {
                            make.right.equalTo(self);
                        }else
                        {
                            make.width.equalTo(self).multipliedBy(0.5).offset(-jianGe);
                            model.uiType = UITypeForLeft;
                        }
                    }
                }else{
                    make.top.equalTo(self);
                    make.left.equalTo(self);
                    if (model.uiType == UITypeForCenter) {
                        make.right.equalTo(self);
                    }else
                    {
                        make.width.equalTo(self).multipliedBy(0.5).offset(-jianGe);
                        model.uiType = UITypeForLeft;
                    }
                }
                make.height.equalTo(@(t_viewH));
        }];
        }else{
            t_viewH = r_viewH;
            [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self);
                if (l_view) {
                    make.top.equalTo(l_view.mas_bottom);
                }else{
                    make.top.equalTo(self);
                }
                make.height.equalTo(@(t_viewH));
            }];
        }
        
    }
    [l_view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-20);
        
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - 标尺代理方法

- (void)rulerWith:(NSInteger)days  andRulerType:(RulerType)_rulerType andView:(UIView *)view{
    RulerShowView * tmpView = (RulerShowView*)view.superview;
    tmpView.valueText.text = [NSString stringWithFormat:@"%ld",days];
    
    //即时打印出标尺滑动位置的数值
    NSLog(@"当前刻度值：%ld",days);
}

- (void)rulerRunEnd {
    NSLog(@"end");
}

#pragma mark - textFeildDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    int index = (int)textField.tag-Text_Tag;
    if (textField.becomeFirstResponder) {
        UITextField * text = [self viewWithTag:Text_Tag+index+1];
        [textField resignFirstResponder];
        [text becomeFirstResponder];
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{
    
    return YES;
    
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    
    return YES;
}
@end
